
import {history, post, message, get} from '@/utils/utils'

const settlementPrice = {
  namespaced: true,
  state: {
    newRate: ''
  },

  mutations: {
    save(state, { field, payload }) {
      if (Object.prototype.toString.call(payload) === '[object Object]') {
        state[field] = { ...state[field], ...payload }
      } else {
        state[field] = payload
      }
    }
  },

  actions: {
    updateSettlementPrice({ commit, dispatch }, {newRate, type, agentNo, modifyAgentNo}) {
      const body = {
        newRate
      }

      if (type === 'default') {
        body.modifyType = '2'
        body.agentNo = agentNo
      } else {
        body.modifyType = '4'
        body.agentNo = agentNo
        body.modifyAgentNo = modifyAgentNo
      }

      post({api: '/agt/agentratemodify/mofiyRate',
        data: body,
        success: (data) => {
          if (type === 'default') {
            commit('home/save', {field: 'agentInfo', payload: {nextAgentRate: newRate}}, {root: true})
          } else {
            commit('managementDetail/save', {field: 'detail', payload: {rate: newRate}}, {root: true})
          }

          message.success('修改成功')
          setTimeout(() => history.goBack(), 1500)
        }})
    },

    changeNewRate({ commit, state }, payload) {
      const {newRate: oldRate} = state
      let rate = payload

      if (rate > 0.6) rate = oldRate

      commit('save', {field: 'newRate', payload: rate})
    }
  }
}

export default settlementPrice
